﻿using AnesSystem.Models;
using Common;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AnesSystem.Services
{
    public class PatientLinkService : BaseService<PatientLinkService, PatientLinkModel>
    {
        #region 返回值为FilterDefinition<BsonDocument>
        //已进入手术间
        private FilterDefinition<BsonDocument> SeachInRoom()
        {
            return Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM);
        }
        private FilterDefinition<BsonDocument> SeachScheduled()
        {
            return Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED);
        }

        private FilterDefinition<BsonDocument> SeachOneMonthScheduledByDays(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", start),
                    Builders<BsonDocument>.Filter.Lt("Schedule.Jhsssj", end)));
        }
        //查询状态正常
        private FilterDefinition<BsonDocument> SeachFlagValid()
        {
            return Builders<BsonDocument>.Filter.Eq("Patient.Status", Constants.FLAG_VALID);
        }
        //已进入PCU
        private FilterDefinition<BsonDocument> SeachInPCU()
        {
            return Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU);
        }
        //已排班，时间为前三天到今天
        private FilterDefinition<BsonDocument> SeachScheduledByDays(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", start),
                    Builders<BsonDocument>.Filter.Lt("Schedule.Jhsssj", end)));
        }
        private FilterDefinition<BsonDocument> SeachScheduledToDays(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", start),
                    Builders<BsonDocument>.Filter.Lt("Schedule.Jhsssj", end)));
        }
        //查询明天已排班
        private FilterDefinition<BsonDocument> SeachTomorrowScheduledByDays(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", start),
                    Builders<BsonDocument>.Filter.Lt("Schedule.Jhsssj", end)));
        }
        private FilterDefinition<BsonDocument> SeachByInRoom(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Status.InRoomTime", start),
                    Builders<BsonDocument>.Filter.Lt("Status.InRoomTime", end)));
        }
        //已出手术间，根据时间
        private FilterDefinition<BsonDocument> SeachByOutRoom(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Status.OutRoomTime", start),
                    Builders<BsonDocument>.Filter.Lt("Status.OutRoomTime", end)));
        }
        //已出PCU，根据时间
        private FilterDefinition<BsonDocument> SeachByOutPCU(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Status.OutPacuTime", start),
                    Builders<BsonDocument>.Filter.Lt("Status.OutPacuTime", end)));
        }
        private FilterDefinition<BsonDocument> SeachByInPCU(int startDays, int endDays)
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(startDays);
            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(endDays);
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.Gte("Status.InPacuTime", start),
                    Builders<BsonDocument>.Filter.Lt("Status.InPacuTime", end)));
        }
        //根据历史状态查询
        private FilterDefinition<BsonDocument> SeachByStatus()
        {
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Ne("Patient.Status", Constants.FLAG_IN_VALID),
                Builders<BsonDocument>.Filter.Or(
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)));
        }

        //根据手术间查询
        private FilterDefinition<BsonDocument> SeachByRoom(string room)
        {
            return Builders<BsonDocument>.Filter.Eq("Schedule.Pbssjh", room);
        }

        //根据住院号查询
        private FilterDefinition<BsonDocument> SeachByPatientid(string patientid)
        {
            return Builders<BsonDocument>.Filter.Eq("Schedule.Zych", patientid);
        }

        //根据姓名查询
        private FilterDefinition<BsonDocument> SeachByBrxm(string name)
        {
            return Builders<BsonDocument>.Filter.Eq("Patient.Brxm", name);
        }
        //根据医生查询
        private FilterDefinition<BsonDocument> SeachByOperDoc(ObjectId operDoc)
        {
            return Builders<BsonDocument>.Filter.Or(
                 Builders<BsonDocument>.Filter.Eq("AnesthesiaRecord.Zmys", operDoc),
                 Builders<BsonDocument>.Filter.Eq("AnesthesiaRecord.Fmys", operDoc),
                 Builders<BsonDocument>.Filter.Eq("AnesthesiaRecord.OperDoctor1", operDoc),
                 Builders<BsonDocument>.Filter.Eq("AnesthesiaRecord.OperDoctor2", operDoc),
                 Builders<BsonDocument>.Filter.Eq("AnesthesiaRecord.OperDoctor3", operDoc)
                );
        }
        //根据开始时间和结束时间查询创建时间
        private FilterDefinition<BsonDocument> SeachByDate(DateTime startTime, DateTime endTime)
        {
            return Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Gte("Status.OutRoomTime", startTime),
                 Builders<BsonDocument>.Filter.Lt("Status.OutRoomTime", endTime));
        }

        #endregion

        //病人信息修改
        public void PatientUpdate(PatientLinkModel patientLinkModel)
        {
            long num = HelperManager.Instance.MongoDB.Update(patientLinkModel, "patientLink").ModifiedCount;
        }
        //根据时间查询已排班手术
        public List<PatientLinkModel> SechudlePatientByTime(DateTime startTime, DateTime endTime)
        {

            var query = Builders<BsonDocument>.Filter.And(
                    Builders<BsonDocument>.Filter.And(Builders<BsonDocument>.Filter.Eq("Patient.Status", Constants.FLAG_VALID),
                    Builders<BsonDocument>.Filter.Or(
                        Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                        Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU),
                        Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                        Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                        Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED)
                        ),
                    //Builders<BsonDocument>.Filter.And(Builders<BsonDocument>.Filter.Ne("Application.Detailstatus", Constants.AP_STATUS_MAZUIQIAN),
                    Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", startTime),
                    Builders<BsonDocument>.Filter.Lt("Schedule.Jhsssj", endTime)));
            return FindAll(query).ToList();
        }
        //根据住院号查询
        public List<PatientLinkModel> GetPatientByZyh(string zyh)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Eq("Patient.Zyh", zyh));
            if (FindAll(query).ToList() == null || FindAll(query).ToList().Count < 1)
            {
                return null;
            }
            return FindAll(query).ToList();
        }

        //获取特定手术间是否存在术中病人
        public bool IsOperInRoom(string operRoom)
        {
            if (string.IsNullOrEmpty(operRoom))
            {
                return false;
            }
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                                                        filter.Eq("Schedule.Pbssjh", operRoom),
                                                        filter.Ne("Patient.Status", Constants.FLAG_IN_VALID));
            List<PatientLinkModel> ret = FindAll(query).ToList();
            if (ret == null || ret.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //获取特定手术间的术中病人
        public List<PatientLinkModel> InOperInRoom(string operRoom)
        {
            if (string.IsNullOrEmpty(operRoom))
            {
                return null;
            }
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                                                        filter.Eq("Schedule.Pbssjh", operRoom),
                                                        filter.Ne("Patient.Status", Constants.FLAG_IN_VALID));
            List<PatientLinkModel> ret = FindAll(query).ToList();
            if (ret == null || ret.Count == 0)
            {
                return null;
            }
            else
            {
                return ret;
            }
        }
        public new PatientLinkModel GetObjById(string id)
        {
            List<PatientLinkModel> ret = (List<PatientLinkModel>)FindAll(Builders<BsonDocument>.Filter.Eq("_id", new ObjectId(id)));
            if (ret != null && ret.Count != 0)
            {
                return ret[0];
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 获取选择手术
        /// </summary>
        /// <returns></returns>
        public List<PatientLinkModel> GetAllSelTableOpers()
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
               SeachFlagValid(),
               filter.Or(
                    SeachInRoom(),
                    SeachInPCU(),
                    SeachByOutRoom(0, 1),
                    SeachOneMonthScheduledByDays(-30, 30),
                    SeachByOutPCU(0, 1)
                    ));
            return FindAll(query).ToList();
        }
        public List<PatientLinkModel> GetSelTableOpers()
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
               SeachFlagValid(),
               filter.Or(
                    SeachInRoom(),
                    SeachInPCU(),
                    SeachByOutRoom(0, 1),
                    SeachScheduledByDays(0, 1),
                    SeachByOutPCU(0, 1)
                    ));
            return FindAll(query).ToList();
        }

        public List<PatientLinkModel> GetSelTableOper()
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
               SeachFlagValid(),
               filter.Or(
                    SeachInRoom(),
                    SeachInPCU(),
                    SeachByOutRoom(0, 1),
                    SeachScheduledByDays(0, 1),
                    SeachByOutPCU(0, 1)
                    ));
            return FindAll(query).ToList();
        }
        /// <summary>
        /// 获取明天选择手术
        /// </summary>
        /// <returns></returns>
        public List<PatientLinkModel> GetTomorrowSelTableOpers()
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
               SeachFlagValid(),
               filter.Or(
                    SeachTomorrowScheduledByDays(1, 5)
                    ));
            return FindAll(query).ToList();
        }
        //根据手术间获取手术
        public List<PatientLinkModel> GetScheduleTableOpersByRoomId(string room)
        {
            var filter = Builders<BsonDocument>.Filter;
            int days = -3;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Patient.Status", Constants.FLAG_VALID),
                filter.Eq("Schedule.Pbssjh", room),
                    Builders<BsonDocument>.Filter.And(
                                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                                                    Builders<BsonDocument>.Filter.And(
                                                        filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(days)),
                                                        filter.Lte("Schedule.Jhsssj", DateTime.Now.AddDays(2)))));
            return FindAll(query).ToList();
        }

        //基础统计
        public IEnumerable<PatientLinkModel> GetBaseCount(DateTime startTime, DateTime endTime)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.Or(
                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU));
            if (null != startTime && null != endTime)
            {
                query = Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Or(
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)),
                Builders<BsonDocument>.Filter.And(
                    filter.Gte("Status.OutRoomTime", startTime),
                    filter.Lt("Status.OutRoomTime", endTime))
                );
            }
            else if (null != startTime)
            {
                query = Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Or(
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)),
                Builders<BsonDocument>.Filter.And(
                    filter.Gte("Status.OutRoomTime", startTime))
                );
            }
            else if (null != endTime)
            {
                query = Builders<BsonDocument>.Filter.And(
                Builders<BsonDocument>.Filter.Or(
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)),
                Builders<BsonDocument>.Filter.And(
                    filter.Gte("Status.OutRoomTime", startTime)));
            }

            return FindAll(query).ToList();
        }

        //获取未排台手术数据
        public List<PatientLinkModel> GetNoScheduleTableOpers(DateTime selDateTime)
        {
            DateTime startDate = new DateTime(selDateTime.Year, selDateTime.Month, selDateTime.Day);
            DateTime endDate = new DateTime(selDateTime.Year, selDateTime.Month, selDateTime.Day).AddDays(1);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_APPLIED),
                Builders<BsonDocument>.Filter.Gte("Application.Yysssj", startDate),
                Builders<BsonDocument>.Filter.Lte("Application.Yysssj", endDate));
            return FindAll(query).ToList();
        }
        //历史手术，根据手术出室时间查询，且已经手术出室，或者出PCU
        public List<PatientLinkModel> GetFinishOperByTime(DateTime start, DateTime end)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
            filter.Ne("Patient.Status", Constants.FLAG_IN_VALID),
            filter.Or(
                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)),
            Builders<BsonDocument>.Filter.And(
                filter.Gte("Status.OutRoomTime", start),
                filter.Lte("Status.OutRoomTime", end))
            );


            //var query = Builders<BsonDocument>.Filter.And(
            //    Builders<BsonDocument>.Filter.And(
            //        filter.Gte("Status.InRoomTime", start),
            //        filter.Lte("Status.InRoomTime", end)),
            //    Builders<BsonDocument>.Filter.And(
            //        filter.In("Application.Detailstatus", new List<FieldDefinition<BsonDocument>>() { Constants.OR_STATUS_FLAG_OUT_ROOM, Constants.OR_STATUS_FLAG_OUT_PCU }),
            //        filter.Ne("Patient.Status", Constants.FLAG_IN_VALID)
            //        ));
            return FindAll(query).ToList();
        }

        #region //引用为0的方法
        //获取历史手术数据 ,历史手术查询 
        public List<PatientLinkModel> GetHistoryOperations(DateTime startTime, DateTime endTime, string room, string zyh, string name)
        {
            var query = Builders<BsonDocument>.Filter;
            if (!string.IsNullOrEmpty(room))//如果手术间不为空
            {
                query.And(SeachByRoom(room));
            }
            if (!string.IsNullOrEmpty(zyh))//如果住院号不为空
            {
                query.And(SeachByPatientid(zyh));
            }
            if (!string.IsNullOrEmpty(name))//如果姓名不为空
            {
                query.And(SeachByBrxm(name));
            }
            var queryFind = query.And(
                SeachByDate(startTime, endTime),
                SeachByStatus()
                );
            //var queryFind= Builders<PatientLinkModel>.Filter.And(
            //    Builders<PatientLinkModel>.Filter.Gte("Status.OutRoomTime", startTime),
            //     Builders<PatientLinkModel>.Filter.Lt("Status.OutRoomTime", endTime));

            return FindAll(queryFind).ToList();



            ////如果手术间不为空
            //if (!string.IsNullOrEmpty(room))
            //{
            //    var query = Builders<BsonDocument>.Filter.And(
            //        SeachByRoom(room),
            //        SeachByDate(startTime, endTime),
            //        SeachByStatus());
            //    return FindAll(query).ToList();
            //}
            //else if (!string.IsNullOrEmpty(zyh))//如果住院号不为空
            //{
            //    var query = Builders<BsonDocument>.Filter.And(
            //        SeachByDate(startTime, endTime),
            //        SeachByPatientid(zyh),
            //        SeachByStatus());
            //    return FindAll(query).ToList();
            //}
            //else if (!string.IsNullOrEmpty(name))//如果姓名不为空
            //{
            //    var query = Builders<BsonDocument>.Filter.And(
            //        SeachByDate(startTime, endTime),
            //        SeachByBrxm(name),
            //        SeachByStatus());
            //    return FindAll(query).ToList();
            //}
            //else if (startTime != null && endTime != null)//其他都为空，除了时间
            //{
            //    var query = Builders<BsonDocument>.Filter.And(
            //        SeachByDate(startTime, endTime),
            //        Builders<BsonDocument>.Filter.Ne("Patient.Status", Constants.FLAG_IN_VALID),
            //    Builders<BsonDocument>.Filter.Or(
            //    Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
            //    Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)));
            //    return FindAll(query).ToList();
            //}
            //else
            //    return null;

        }

        //根据手术状态获取手术
        public List<PatientLinkModel> GetSsztTableOpers(string sszt)
        {
            var filter = Builders<BsonDocument>.Filter;
            int days = -4;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Patient.Status", Constants.FLAG_VALID),
                Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", sszt)),
                Builders<BsonDocument>.Filter.Or(
                    Builders<BsonDocument>.Filter.And(
                                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                                                    Builders<BsonDocument>.Filter.And(
                                                        filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(days)),
                                                        filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                    Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(-1)),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                    Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(days)),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                    Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(-1)),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                    Builders<BsonDocument>.Filter.And(
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                                                    Builders<BsonDocument>.Filter.And(
                                                        filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(days)),
                                                        filter.Lte("Schedule.Jhsssj", DateTime.Now)))));

            return FindAll(query).ToList();
        }

        //获取3天内所有未排班和排班的手术数据
        public List<PatientLinkModel> GetAllTableOpers()
        {
            DateTime time = DateTime.Now;
            DateTime endDate = time;
            DateTime startDate = time.AddDays(-3);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Gte("Application.yysssj", startDate),
                Builders<BsonDocument>.Filter.Lte("Application.yysssj", endDate),
                Builders<BsonDocument>.Filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_APPLIED)
                | filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED));
            return FindAll(query).ToList();
        }

        /// <summary>
        /// 查询手术根据时间段
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<PatientLinkModel> GetOperationsByDay(DateTime start, DateTime end)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = filter.And(
                filter.Eq("Patient.Status", Constants.FLAG_VALID),
                filter.Or(
                     filter.And(
                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_APPLIED),
                                    filter.Gte("Application.Yysssj", start),
                                    filter.Lte("Application.Yysssj", end)),
                     filter.And(
                                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                                                    filter.And(
                                                        filter.Gte("Schedule.Jhsssj", start),
                                                        filter.Lte("Schedule.Jhsssj", end))),
                      filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                                                filter.And(
                                                    filter.Gte("Schedule.Jhsssj", start),
                                                    filter.Lte("Schedule.Jhsssj", end))),
                      filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU),
                                                filter.And(
                                                    filter.Gte("Schedule.Jhsssj", start),
                                                    filter.Lte("Schedule.Jhsssj", end))),
                       filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                                                    filter.And(
                                                        filter.Gte("Schedule.Jhsssj", start),
                                                        filter.Lte("Schedule.Jhsssj", end))),
                     filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                                                filter.And(
                                                    filter.Gte("Schedule.Jhsssj", start),
                                                    filter.Lte("Schedule.Jhsssj", end)))));
            return FindAll(query).ToList();
        }

        //获取明天手术数据
        public List<PatientLinkModel> GetMingTTableOpers(int date)
        {
            DateTime time = DateTime.Now;
            DateTime endDate = time;
            DateTime startDate = time.AddDays(-3);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("patient.status", Constants.FLAG_VALID),
                Builders<BsonDocument>.Filter.Or(
                     Builders<BsonDocument>.Filter.And(
                                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                                                    Builders<BsonDocument>.Filter.And(
                                                        filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(date)),
                                                        filter.Lte("Schedule.Jhsssj", DateTime.Now)),
                      Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(date)),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                      Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(date)),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now))),
                       Builders<BsonDocument>.Filter.And(
                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                                                    Builders<BsonDocument>.Filter.And(
                                                        filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(date)),
                                                        filter.Lte("Schedule.Jhsssj", DateTime.Now)))),
                     Builders<BsonDocument>.Filter.And(
                                                filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                                                Builders<BsonDocument>.Filter.And(
                                                    filter.Gte("Schedule.Jhsssj", DateTime.Now),
                                                    filter.Lte("Schedule.Jhsssj", DateTime.Now.AddDays(date)))))
                    );
            return FindAll(query).ToList();
        }

        //获取已排班的手术数据
        public List<PatientLinkModel> GetScheduleTableOpers(int days = 0)
        {
            DateTime temp = DateTime.Now.AddDays(days);
            DateTime startDate = new DateTime(temp.Year, temp.Month, temp.Day);
            DateTime endDate = new DateTime(startDate.Year, startDate.Month, startDate.Day).AddDays(days + 1);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
               Builders<BsonDocument>.Filter.And(filter.Eq("Patient.Status", Constants.FLAG_VALID),
            Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", startDate),
            Builders<BsonDocument>.Filter.Lte("Schedule.Jhsssj", endDate)));
            return FindAll(query).ToList();
        }

        //获取5天内所有排班的手术数据
        public List<PatientLinkModel> GetTableOpers(int days = 0)
        {
            DateTime temp = DateTime.Now.AddDays(days);
            DateTime startDate = new DateTime(temp.Year, temp.Month, temp.Day);
            DateTime endDate = new DateTime(startDate.Year, startDate.Month, startDate.Day).AddDays(days + 1);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Or(filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED), filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_APPLIED)),
                Builders<BsonDocument>.Filter.And(filter.Eq("Patient.Status", Constants.FLAG_VALID),
                Builders<BsonDocument>.Filter.Gte("Schedule.Jhsssj", DateTime.Now.AddDays(days)),
                Builders<BsonDocument>.Filter.Lte("Schedule.Jhsssj", DateTime.Now.AddDays(days + 1))));
            return FindAll(query).ToList();
        }

        /// <summary>
        /// 根据时间获取未排班和已排班手术
        /// </summary>
        /// <param name="time"></param>
        /// <param name="days"></param>
        /// <returns></returns>
        public List<PatientLinkModel> GetTableOpers(DateTime time, int days = 0)
        {
            DateTime startDate = new DateTime(time.Year, time.Month, time.Day);
            DateTime endDate = new DateTime(startDate.Year, startDate.Month, startDate.Day).AddDays(days + 1);
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                            filter.Eq("Patient.Status", Constants.FLAG_VALID),
                            filter.Or(
                                filter.And(
                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_APPLIED),
                                    filter.Gte("Application.Yysssj", startDate),
                                    filter.Lte("Application.Yysssj", endDate)),
                                filter.And(
                                    filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_SCHEDULED),
                                    filter.Gte("Schedule.Jhsssj", startDate),
                                    filter.Lte("Schedule.Jhsssj", endDate))));
            return FindAll(query).ToList();
        }

        //获取当前手术间手术台数最大的一个
        public long FindAnesID()
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Or(
                            filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_ROOM),
                            filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_ROOM),
                            filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_IN_PCU),
                            filter.Eq("Application.Detailstatus", Constants.OR_STATUS_FLAG_OUT_PCU)));
            return FindCount(query);
        }
        #endregion




    }
}